package com.haowan.accessibilityservice;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<ItemEntity> mList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View crushView;
        ImageView crushImage;
        TextView crushName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            crushView = itemView;
            crushImage = (ImageView) itemView.findViewById(R.id.crush_image);
            crushName = (TextView) itemView.findViewById(R.id.crush_name);
        }
    }

    public MyAdapter(List<ItemEntity> list) {
        mList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.crushView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                ItemEntity crush = mList.get(position);
                Toast.makeText(v.getContext(), "you clicked view " + crush.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.crushImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAbsoluteAdapterPosition();
                ItemEntity crush = mList.get(position);
                Toast.makeText(v.getContext(), "you clicked image " + crush.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemEntity crush = mList.get(position);
        holder.crushImage.setImageResource(crush.getImageId());
        holder.crushName.setText(crush.getName());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
}
